import constant from "../../utils/constant";
import { request } from "../../utils/request";
const BASE_URL = 'http://localhost:8080/';
interface IqyLoginRes {
  code: string,
  errMsg: string,
}
const qyLogin = async () => {
  return new Promise<IqyLoginRes>((resolve, reject) => {
    return;
    (wx as any).qy.login({
      success: (loginRes: IqyLoginRes) => {
        resolve(loginRes);
      },
      fail: () => {
        wx.hideLoading();
        //  登录失败
        reject({ code: undefined })
      }
    });
  })
}
const login = async () => {
  // const { code } = await qyLogin()
  const code = ''
  if (typeof code == "undefined") {
    // 登录失败 
    return;
  }
  const { data: { token } } = await request<API.Result<{ token: string }>>({
    url: BASE_URL + 'api/login', data: { code, }, method: "POST"
  })
  wx.setStorageSync(constant.SYS_TOKEN, token);
  const { data } = await request<API.Result<User.userInfo>>({ url: BASE_URL + 'api/userInfo', data: token, method: "POST" })
  wx.setStorageSync(constant.ROLE_LIST, data.roles)
  wx.setStorageSync(constant.USER_INFO, data)
  return data;
}

export default { login }